﻿using System;
using System.Collections;
using System.IO;
using System.Windows.Forms;
using System.Xml;

public class TreeViewHelper
{
    /// <summary>
    /// 添加文件夹 文件
    /// </summary>
    /// <param name="tnSubNode"></param>
    //public static int AddAllRecuer(TreeNode tnSubNode)
    //{
    //    treeViewDirect.BeginUpdate();
    //    tnSubNode.Nodes.Clear();

    //    int i = 0;

    //    if (tnSubNode.Tag != null)
    //    {
    //        if (TreeViewHelper.GetType(tnSubNode) == "d")
    //        {
    //            DirectoryInfo diRoot = new DirectoryInfo(Path.Combine(TreeViewHelper.GetPath(tnSubNode), ""));
    //            DirectoryInfo[] dirs = diRoot.GetDirectories();

    //            iDirectories = 0;
    //            foreach (DirectoryInfo dir in dirs)
    //            {
    //                iDirectories++;
    //                TreeNode subNode = new TreeNode(dir.Name);

    //                ArrayList arrayListTag = new ArrayList();
    //                arrayListTag.Add(dir.FullName);
    //                arrayListTag.Add("d");
    //                subNode.Tag = arrayListTag;
    //                TreeViewHelper.ChangeFileIco(subNode);

    //                tnSubNode.Nodes.Add(subNode);

    //                i++;

    //                i=AddAllRecuer(subNode);

    //            }

    //            FileInfo[] theFiles = diRoot.GetFiles();
    //            foreach (FileInfo theFile in theFiles)
    //            {
    //                iFiles++;

    //                TreeNode subNode = new TreeNode(theFile.Name);

    //                ArrayList arrayListTag = new ArrayList();
    //                arrayListTag.Add(theFile.FullName);
    //                arrayListTag.Add("f");
    //                subNode.Tag = arrayListTag;
    //                TreeViewHelper.ChangeFileIco(subNode);

    //                i++;

    //                tnSubNode.Nodes.Add(subNode);
    //            }

    //        }
    //    }

    //    treeViewDirect.EndUpdate();

    //    return i;
    //}

    public static int GetNodesNumber(TreeNode tn)
    {
        int i = 0;
        foreach (TreeNode tn1 in tn.Nodes)
        {
            i++;

            i += GetNodesNumber(tn1);
        }

        return i;
    }

    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="tnSubNode"></param>
    public static int AddDirectories(TreeNode tnSubNode)
    {
        // This method is used to get directories (from disks, or from other directories)

        //treeViewDirect.BeginUpdate();
        int iDirectories = 0;
        //MessageBox.Show("1");
        try
        {
            DirectoryInfo diRoot;
            diRoot = new DirectoryInfo(Path.Combine(((ArrayList)tnSubNode.Tag)[0].ToString(), ""));
            DirectoryInfo[] dirs = diRoot.GetDirectories();

            // Add the sub directories to the treeViewDirect
            foreach (DirectoryInfo dir in dirs)
            {
                if (dir.Name == ".svn")
                {
                    continue;
                }

                iDirectories++;
                TreeNode subNode = new TreeNode(dir.Name);

                subNode.ImageKey = "dirClose";
                subNode.SelectedImageKey = "dirOpen";

                ArrayList arrayListTag = new ArrayList();
                arrayListTag.Add(dir.FullName);
                arrayListTag.Add("d");
                subNode.Tag = arrayListTag;

                tnSubNode.Nodes.Add(subNode);
            }

            return iDirectories;
        }
        catch (Exception ee)
        {
            return 0;
        }
    }

    public static string GetType(TreeNode Node)
    {
        return ((ArrayList)Node.Tag)[1].ToString();
    }

    public static string GetPath(TreeNode Node)
    {
        return ((ArrayList)Node.Tag)[0].ToString();
    }

    /// <summary>
    /// 添加文件
    /// </summary>
    /// <param name="strPath"></param>
    public static int AddFiles(TreeNode tnSubNode)
    {
        //TreeNode tnSelected = treeViewDirect.SelectedNode;
        int iFiles = 0;

        DirectoryInfo diRoot;
        diRoot = new DirectoryInfo(Path.Combine(((ArrayList)tnSubNode.Tag)[0].ToString(), ""));
        FileInfo[] theFiles = diRoot.GetFiles();

        foreach (FileInfo theFile in theFiles)
        {
            if (Path.GetExtension(theFile.Name) == ".bak" || Path.GetExtension(theFile.Name) == ".db")
            { continue; }

            if (Path.GetExtension(theFile.Name) == ".tmp" || theFile.Name.Substring(0, 1) == "~")
            { continue; }

            iFiles++;

            TreeNode subNode = new TreeNode(theFile.Name);

            ChangeFileIco(subNode);

            ArrayList arrayListTag = new ArrayList();
            arrayListTag.Add(theFile.FullName);
            arrayListTag.Add("f");
            subNode.Tag = arrayListTag;

            tnSubNode.Nodes.Add(subNode);
        }

        //treeViewDirect.SelectedNode = tnSelected;

        return iFiles;
    }

    /// <summary>
    /// 加载
    /// </summary>
    /// <param name="subNode"></param>
    public static void ChangeFileIco(TreeNode subNode)
    {
        if (subNode.Tag != null)
        {
            if (((ArrayList)subNode.Tag)[1].ToString() == "d")
            {
                subNode.ImageKey = "dirOpen";
                subNode.SelectedImageKey = "dirClose";
                return;
            }
        }

        string strPath = "Xml/Img.xml";
        XmlDocument doc = new XmlDocument();
        doc.Load(strPath);

        XmlNodeList nodeList = doc.SelectSingleNode("data").ChildNodes;

        string Extension = "";
        string ImageKey = "";
        string SelectedImageKey = "";
        //string ImageKeyPath = "";
        //string SelectedImageKeyPath = "";

        foreach (XmlNode xn in nodeList)
        {
            if (xn.Name == "row")
            {
                //string[] extensions={};
                foreach (XmlNode xn1 in xn.ChildNodes)
                {
                    if (xn1.Name == "Extension")
                    {
                        ImageKey = "";
                        SelectedImageKey = "";

                        Extension = xn1.InnerText;

                        ////添加几种文件图标的统一设置。
                        //extensions = Extension.Split(' ');
                    }
                    else if (xn1.Name == "ImageKey")
                    {
                        ImageKey = xn1.InnerText;
                    }
                    else if (xn1.Name == "SelectedImageKey")
                    {
                        SelectedImageKey = xn1.InnerText;
                    }
                    else
                    {
                        continue;
                    }
                }

                //foreach (string extension in extensions)
                //{
                if (Path.GetExtension(subNode.Text).ToLower() == Extension && SelectedImageKey != "" && ImageKey != "")
                {
                    //if (Extension == ".java") {
                    //}
                    subNode.ImageKey = ImageKey;
                    subNode.SelectedImageKey = SelectedImageKey;

                    return;
                }
                //}
            }
        }

        subNode.ImageKey = "unknow";
        subNode.SelectedImageKey = "zz_unknow";
    }
}